package com.example.chapter06;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter06.database.UserDBHelper;
import com.example.chapter06.entity.User;
import com.example.chapter06.util.ToastUtil;

import java.time.LocalDateTime;
import java.util.List;

public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private UserDBHelper mHelper;
    private CheckBox ck_married;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_helper);

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);

        ck_married = findViewById(R.id.ck_married);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_alter).setOnClickListener(this);
        findViewById(R.id.btn_select).setOnClickListener(this);

        //此处初始化界面的时候会赋值，点击按钮后不会进行赋值
        /*
        *name = et_name.getText().toString();
        age = et_age.getText().toString();
        height = et_height.getText().toString();
        weight = et_weight.getText().toString();
        Log.d("liu",name);*/



    }

    @Override
    protected void onStart() {
        super.onStart();
        //获得数据库帮助器的实例
        mHelper = UserDBHelper.getInstance(this);
        //打开数据库帮助器的读写链接
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //关闭数据库链接
        mHelper.closeLink();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();

        if (v.getId() == R.id.btn_save) {
            user = new User(name,
                    Integer.parseInt(age),
                    Long.parseLong(height),
                    Float.parseFloat(weight),
                    ck_married.isChecked(),
                    LocalDateTime.now());
            if (mHelper.insert(user) > 0) {
                ToastUtil.show(this, "添加成功");
            }

        }
        if (v.getId() == R.id.btn_delete) {
            if (mHelper.deleteByName(name, age) > 0) {
                ToastUtil.show(this, "删除成功");
            }
        }

        if (v.getId() == R.id.btn_alter) {
            if (mHelper.update(user) > 0){
                ToastUtil.show(this,"修改成功");
            }
        }

        if(v.getId() == R.id.btn_select){
            //List<User> list = mHelper.queryAll();
            List<User> list = mHelper.queryByName(name);
            for (User u : list) {
                Log.d("liu",u.toString());
            }
        }
    }
}